
var code=require('../txtCode/code.js');
var moment=require('moment');

var Notepad = require('../models/notepadModel');

// var async=require('async');
module.exports={
	Get:function(req,res,next){	
			Notepad
			.find()
			.then(
				function(result){
						// console.log(result);
						console.log(" 有人来getnote ");
						// if(result.length==0) result=[{title:1},{title:2}];
						console.log(result[0]);
						res.send({code:code.success,data:result});
					}   
				,function(err){
					if(err) console.log(err);
					res.send({code:code.findFail});
			});
	},
	Add:function(req,res,next){	
			var data=req.body;
     		
     		console.log("存储");
     		console.log(data);
     		// console.log(req.query.title);
     		// res.send({code:'haha'});
     		var oNewNote=new Notepad(data);
    		 	oNewNote.save(function(err){
    		 			if (err) {
    		 				console.log(err);
    		 			}else{
    		 				res.send({code:code.success,message:"add success"});
    		 			} ;
    			});//save()

	},
	update:function(req,res,next){	
			var data=req.body;

     		Notepad
     		.update({title:data.title},{$set:data},function(err,docs){
     			if(err)  
     			{	
     				console.log(err);
     				res.send({code:code.findFail})
     			}else{
     				res.send({code:code.success})
     			}
     		});
	},
	Remove:function(req,res,next){
			var data = req.body; 
			Notepad
			.remove(data,function(err,result){
				if(err) console.log(err);
				res.send({code:code.success,message:"delete success"});
			})
	},
	Find:function(req,res,next){
			var data = req.body; 

			Notepad
			.find(data,function(err,result){
				if(err) console.log(err);
				res.send({code:code.success,data:result});
			})
	},
	Update:function(req,res,next){
			var data = req.body; 
			console.log("看下id");
			console.log(data);
			Notepad
			.update({_id:data._id},data,function(err,result){
				if(err) console.log(err);
				res.send({code:code.success,data:result});
			})
	},
}